<%= render "users/additional_authentication" %>

<%= render "users/account_set_password" %>

<%= render "users/account_providers_emails" %>

<div class="crayons-card crayons-card--content-rows">
  <h2 class="crayons-subtitle-1">
    <%= t("views.settings.account.export.heading") %>
  </h2>

  <% if @user.export_requested? %>
    <div class="crayons-notice crayons-notice--warning" aria-live="polite">
      <p><%= t("views.settings.account.export.requested") %></p>
    </div>
  <% else %>
    <p class="-mt-2"><%= t("views.settings.account.export.desc") %></p>

    <%= form_for(@user, html: { id: nil }) do |f| %>
      <div class="crayons-field crayons-field--checkbox mb-6">
        <%= f.check_box :export_requested, class: "crayons-checkbox" %>
        <%= f.label :export_requested, t("views.settings.account.export.label"), class: "crayons-field__label" %>
      </div>
      <%= f.hidden_field :tab, value: @tab, id: nil %>
      <div><button type="submit" class="crayons-btn"><%= t("views.settings.account.export.submit") %></button></div>
    <% end %>
  <% end %>
</div>

  <% if @user.base_subscriber? && @user.stripe_id_code.present? %>
    <div class="crayons-card crayons-card--content-rows">
      <h3 class="crayons-subtitle-2">
        DEV++ Subscription
      </h3>
      <div>
        <a href="<%= edit_stripe_subscription_path("me") %>" class="crayons-btn crayon-btn--secondary" data-no-instant>
          Manage Subscription and Billing
        </a>
      </div>
    </div>
  <% elsif @user.base_subscriber? %>
    <div class="crayons-card crayons-card--content-rows">
      <h3 class="crayons-subtitle-2">
        Cancel DEV++ Subscription
      </h3>

      <%= form_with(url: stripe_subscription_path("me"), method: :delete) do |f| %>
        <%= f.label :verification, 'Verify with phrase "pleasecancelmyplusplus"', class: "crayons-field__label" %>
        <%= f.text_field :verification, class: "crayons-textfield mt-1", placeholder: "Put verification phrase here" %>
        <%= f.submit "Delete Subscription", class: "crayons-btn crayon-btn--secondary mt-2" %>
      <% end %>
    </div>
  <% end %>

<div class="crayons-card crayons-card--content-rows">
  <h2 class="crayons-subtitle-1 color-accent-danger">
    <%= t("views.settings.danger") %>
  </h2>

  <% if @user.identities.enabled.size > 1 %>
    <div class="grid gap-2">
      <h3><%= t("views.settings.account.remove.heading") %></h3>
      <p><%= t("views.settings.account.remove.desc1") %></p>
      <p><%= t("views.settings.account.remove.desc2") %></p>
      <ul class="list-disc pl-6">
        <li><%= t("views.settings.account.remove.desc2a") %></li>
        <li><%= t("views.settings.account.remove.desc2b") %></li>
      </ul>
      <p>
        <%= t("views.settings.account.remove.desc3") %>
        <%= render partial: "users/account_providers_settings" %>
      </p>
    </div>

    <% authentication_enabled_providers_for_user.each do |provider| %>
      <% onsubmit = "return confirm('#{t('views.settings.account.remove.confirm', provider: provider.official_name)}');" %>
      <%= form_tag users_remove_identity_path, method: :delete, onsubmit: onsubmit do %>
        <%= hidden_field_tag :provider, provider.provider_name %>

        <button class="crayons-btn crayons-btn--danger crayons-btn--icon-left" type="submit">
          <%= crayons_icon_tag(provider.provider_name, title: provider.official_name) %>
          <%= t("views.settings.account.remove.submit", provider: provider.official_name) %>
        </button>
      <% end %>
    <% end %>
  <% elsif @user.identities.enabled.size == 1 %>
    <div class="grid gap-2">
      <h3><%= t("views.settings.account.remove.heading") %></h3>
      <p><%= t("views.settings.account.remove.unable_to_remove") %></p>
    </div>
  <% end %>

  <div class="grid gap-2">
    <h3><%= t("views.settings.account.delete.heading") %></h3>

    <% if current_user.email? %>
      <%= form_tag user_request_destroy_path, method: :post, autocomplete: "off", id: "delete__account", class: "grid gap-2" do %>
        <p><%= t("views.settings.account.delete.desc1") %></p>
        <ul class="list-disc pl-6">
          <li><%= t("views.settings.account.delete.desc1a") %>
            <%= render partial: "users/account_providers_settings" %>
          </li>

          <%# TODO: expand the delete messaging later %>
          <li><%= t("views.settings.account.delete.desc1b") %></li>
          <li><%= t("views.settings.account.delete.desc1c") %></li>
        </ul>
        <div><button class="crayons-btn crayons-btn--danger" id="delete__account__btn" type="submit"><%= t("views.settings.account.delete.submit") %></button></div>
      <% end %>
    <% else %>
      <p><%= t("views.settings.account.delete.desc2_html", provide: link_to(t("views.settings.account.delete.provide"), "/settings/profile")) %></p>
    <% end %>
  </div>

  <div>
    <p>
      <%= t("contact_prompts.if_any_questions_html") %>
    </p>
  </div>
</div>
